package com.ch.engine.statemachine.machine;

import com.ch.engine.statemachine.enums.Event;
import com.ch.engine.statemachine.enums.State;
import com.google.common.collect.HashBasedTable;

/**
 * 简易版
 */
public class StateMachine {
    /**
     * 用两个key确定一个内容
     */
    private static final HashBasedTable<State, Event, State> hashBasedTable = HashBasedTable.create();

    static {
        hashBasedTable.put(State.UN_SUBMIT, Event.SUBMIT, State.LEADER_CHECK);
        hashBasedTable.put(State.LEADER_CHECK, Event.PASS, State.HR_CHECK);
        hashBasedTable.put(State.LEADER_CHECK, Event.REJECT, State.REJECT);
        hashBasedTable.put(State.HR_CHECK, Event.PASS, State.FINISH);
        hashBasedTable.put(State.HR_CHECK, Event.REJECT, State.REJECT);
    }

    public static State getNext(State state, Event event) {
        State next = hashBasedTable.get(state, event);
        if (next == null) {
            throw new RuntimeException("错误状态");
        }
        return next;
    }
}
